package com.huiquan.icd.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.huiquan.analysis.domain.User;
import com.huiquan.framework.base.ReturnCode;
import com.huiquan.framework.base.ReturnData;
import com.huiquan.framework.utils.RequestParamUtil;
import com.huiquan.framework.utils.ReturnUtil;
import com.huiquan.icd.service.IcdCodeService;

@Controller
public class IcdCodeController {

	@Autowired
	private IcdCodeService icdCodeService;

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/icd/code_list.htm")
	public ModelAndView list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String tabid = req.getParameter("tabid");

		// 维护页面参数到session
		Map<String, Object> pageParam;
		if (tabid != null && !tabid.isEmpty()) {
			pageParam = new HashMap<>();
			pageParam.put("tabid", tabid);
			pageParam.put("start", "1");
			pageParam.put("exactSearchFlag", "0");

			req.getSession().setAttribute("pageParam", pageParam);
		} else {
			pageParam = (Map<String, Object>) req.getSession().getAttribute("pageParam");
		}

		String start = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "start");
		String codeSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "codeSearch");
		String nameSearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "nameSearch");
		String categorySearch = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "categorySearch");
		String exactSearchFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "exactSearchFlag");
		String modifyFlag = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "modifyFlag");
		String countPerPageStr = RequestParamUtil.getParamByRequestOrSession(req, pageParam, "countPerPage");

		return icdCodeService.list(Integer.parseInt(start), codeSearch, nameSearch, categorySearch, exactSearchFlag,
				modifyFlag,countPerPageStr);
	}

	@RequestMapping(value = "/icd/code_pre_edit.htm")
	public ModelAndView preEdit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		Integer start = RequestParamUtil.getIntegerDefault1LargeThan1(req, "start");
		String codeSearch = RequestParamUtil.getTrimString(req, "codeSearch");
		String nameSearch = RequestParamUtil.getTrimString(req, "nameSearch");
		String categorySearch = RequestParamUtil.getTrimString(req, "categorySearch");
		String exactSearchFlag = RequestParamUtil.getTrimString(req, "exactSearchFlag");
		String modifyFlag = RequestParamUtil.getTrimString(req, "modifyFlag");
		String id = req.getParameter("id");
		User user = (User) req.getSession().getAttribute("user");

		return icdCodeService.preEdit(codeSearch, nameSearch, categorySearch, start, id, user, exactSearchFlag,
				modifyFlag);
	}

	@ResponseBody
	@RequestMapping(value = "/icd/code_edit.do")
	public ReturnData edit(HttpServletRequest req, HttpServletResponse resp) {
		String id = req.getParameter("id");
		String changeFlag = req.getParameter("changeFlag");
		String code = RequestParamUtil.getTrimString(req, "code");
		String name = RequestParamUtil.getTrimString(req, "name");
		Integer disable = Integer.parseInt(req.getParameter("disable"));
		String category = RequestParamUtil.getTrimString(req, "category");
		User user = (User) req.getSession().getAttribute("user");

		try {
			return icdCodeService.edit(id, code, name, disable, category, user, changeFlag);
		} catch (Exception e) {
			System.out.println(e.getMessage());
			if (ReturnCode.ICD_CATEGORY_NOT_EXISTS.getDesc().equals(e.getMessage())) {
				return ReturnUtil.fail(ReturnCode.ICD_CATEGORY_NOT_EXISTS);
			}
			return ReturnUtil.fail(ReturnCode.EXCEPTION);
		}
	}

	@ResponseBody
	@RequestMapping(value = "/icd/code_import.do")
	public ReturnData codeImport(HttpServletRequest req, @RequestParam(required = false) MultipartFile file)
			throws Exception {
		User user = (User) req.getSession().getAttribute("user");

		return icdCodeService.codeImport(file, user);
	}

	@RequestMapping(value = "/icd/code_export.htm")
	public void codeExport(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		icdCodeService.codeExport(resp);
	}

	@ResponseBody
	@RequestMapping(value = "/icd/code_get_pname_by_code.do")
	public ReturnData getPnameByCode(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String code = req.getParameter("code");
		return icdCodeService.getPnameByCode(code);
	}

	@ResponseBody
	@RequestMapping(value = "/icd/code_submit_server.do")
	public ReturnData submitServer(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		User user = (User) req.getSession().getAttribute("user");

		return icdCodeService.submitServer(user);
	}
	
}
